package com.qianqian.service.controller;

import com.qianqian.service.config.Logined;
import com.qianqian.service.model.*;
import com.qianqian.service.service.CapitalAccountService;
import com.qianqian.service.service.ZhuanPanService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/zhuanPan")
@EnableAsync
public class ZhuanPan {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @Autowired
    ZhuanPanService zhuanPanService;

    @Autowired
    CapitalAccountService capitalAccountService;

    private final static int xiazhu_second = 25;

    private final static int kaijiang_second = 55;

    private final static int xiazhu_tishi_second = 33;

    private final static String rulecode_dan = "dan";

    private final static String rulecode_shuang = "shuang";

    private final static String rulecode_da = "da";

    private final static String rulecode_xiao = "xiao";

    private final static String rulecode_234 = "x234";

    private final static String rulecode_134 = "x134";

    private final static String rulecode_124 = "x124";

    private final static String rulecode_123 = "x123";


    @PostMapping(value = "/automaticGame", produces = {"application/json;charset=UTF-8"})
    @Async
    public Response automaticGame(@Logined LoginUser loginUser,int gameNumber,String result,Boolean isStop) throws InterruptedException {
        zhuanPanService.automaticGame(loginUser,gameNumber,result,isStop);
        Response response = Response.success();
        return response;
    }
    @PostMapping(value = "/startGame", produces = {"application/json;charset=UTF-8"})
    public Response startGame(@Logined LoginUser loginUser){
        GameResult gameResult = zhuanPanService.newGameNumber(loginUser.getUserId());
        Response response = Response.success();
        response.add("gameNumber",gameResult.getGameNumber());
         response.add("xiaZhuTishiSecond",xiazhu_tishi_second+1);
        return response;
    }
    @PostMapping(value = "/setResult", produces = {"application/json;charset=UTF-8"})
    public Response setResult(@Logined LoginUser loginUser,int gameNumber,String result,Boolean isStop){
        GameResult gameResult = new GameResult();
        gameResult.setResult(result);
        gameResult.setResultDate(new Date());
        gameResult.setUpdateDate(new Date());
        gameResult.setUpdateUserId(loginUser.getUserId());
        int newGameNumber =  zhuanPanService.setResult(gameResult,gameNumber,loginUser,isStop);
        Response response = Response.success();
        response.add("gameNumber",newGameNumber);
        response.add("xiaZhuTishiSecond",xiazhu_tishi_second+1);
        return response;
    }
    @PostMapping(value = "/getXiaZhuTishi", produces = {"application/json;charset=UTF-8"})
    public Response setResult(int gameNumber){
        List<GameRecord> gameRecords = zhuanPanService.selectGameRecordsByGameNumber(gameNumber);
        Map xieZhuTishi = new HashMap<String,Double>();
        xieZhuTishi.put(rulecode_da,0.0);
        xieZhuTishi.put(rulecode_xiao,0.0);
        xieZhuTishi.put(rulecode_dan,0.0);
        xieZhuTishi.put(rulecode_shuang,0.0);
        xieZhuTishi.put(rulecode_234,0.0);
        xieZhuTishi.put(rulecode_134,0.0);
        xieZhuTishi.put(rulecode_124,0.0);
        xieZhuTishi.put(rulecode_123,0.0);
        for(GameRecord gameRecord : gameRecords){
            gameRecord.setChip(gameRecord.getChip()/2);
            switch (gameRecord.getGameRuleCode()){
                case rulecode_da :
                    xieZhuTishi.put(rulecode_xiao,(double)xieZhuTishi.get(rulecode_xiao) + gameRecord.getChip());
                    break;
                case rulecode_xiao :
                    xieZhuTishi.put(rulecode_da,(double)xieZhuTishi.get(rulecode_da) + gameRecord.getChip());
                    break;
                case rulecode_dan :
                    xieZhuTishi.put(rulecode_shuang,(double)xieZhuTishi.get(rulecode_shuang) + gameRecord.getChip());
                    break;
                case rulecode_shuang :
                    xieZhuTishi.put(rulecode_dan,(double)xieZhuTishi.get(rulecode_dan) + gameRecord.getChip());
                    break;
                case "yi" :
                    xieZhuTishi.put(rulecode_234,(double)xieZhuTishi.get(rulecode_234) + gameRecord.getChip());
                    break;
                case "er" :
                    xieZhuTishi.put(rulecode_134,(double)xieZhuTishi.get(rulecode_134) + gameRecord.getChip());
                    break;
                case "san" :
                    xieZhuTishi.put(rulecode_124,(double)xieZhuTishi.get(rulecode_124) + gameRecord.getChip());
                    break;
                case "si" :
                    xieZhuTishi.put(rulecode_123,(double)xieZhuTishi.get(rulecode_123) + gameRecord.getChip());
                    break;
            }
        }
        if((double)xieZhuTishi.get(rulecode_da)>=(double)xieZhuTishi.get(rulecode_xiao)){
            xieZhuTishi.put(rulecode_da,(double)xieZhuTishi.get(rulecode_da)-(double)xieZhuTishi.get(rulecode_xiao));
            xieZhuTishi.put(rulecode_xiao,0.0);
        }else{
            xieZhuTishi.put(rulecode_xiao,(double)xieZhuTishi.get(rulecode_xiao)-(double)xieZhuTishi.get(rulecode_da));
            xieZhuTishi.put(rulecode_da,0.0);
        }
        if((double)xieZhuTishi.get(rulecode_dan)>=(double)xieZhuTishi.get(rulecode_shuang)){
            xieZhuTishi.put(rulecode_dan,(double)xieZhuTishi.get(rulecode_dan)-(double)xieZhuTishi.get(rulecode_shuang));
            xieZhuTishi.put(rulecode_shuang,0.0);
        }else{
            xieZhuTishi.put(rulecode_shuang,(double)xieZhuTishi.get(rulecode_shuang)-(double)xieZhuTishi.get(rulecode_dan));
            xieZhuTishi.put(rulecode_dan,0.0);
        }
        log.info(gameNumber+":"+xieZhuTishi);
        Response response = Response.success();
        response.add("xieZhuTishi",xieZhuTishi);
        response.add("isXieZhuTishi",gameRecords.size()>0);
        return response;
    }
    @PostMapping(value = "/getNewSceneGameResults", produces = {"application/json;charset=UTF-8"})
    public Response getNewSceneGameResults(@Logined LoginUser loginUser){
        Response response = Response.success();
        response.add("sceneGameResults",zhuanPanService.selectNewSceneGameResults());
        return response;
    }
    @PostMapping(value = "/getNewGameNumber", produces = {"application/json;charset=UTF-8"})
    public Response getNewGameNumber(@Logined LoginUser loginUser){
        GameResult gameResult = zhuanPanService.selectNewGameNumber();
        Response response = Response.success();
        response.add("gameStatus",gameResult!=null ? 1 : 0);
        if(gameResult!=null){
            //计算下注秒数
            long interval = (new Date().getTime() - gameResult.getCreateDate().getTime())/1000;
            if(interval>xiazhu_second){
                response.add("xiazhuSecond",0);
            }else{
                response.add("xiazhuSecond",xiazhu_second-interval);
            }
            if(interval>xiazhu_tishi_second){
                response.add("xiaZhuTishiSecond",0);
            }else{
                response.add("xiaZhuTishiSecond",xiazhu_tishi_second-interval);
            }
            //计算开奖秒数
            if(interval>xiazhu_second+kaijiang_second){
                response.add("kaijiangSecond",0);
            }else{
                response.add("kaijiangSecond",xiazhu_second+kaijiang_second-interval);
            }
            response.add("gameRecords",zhuanPanService.selectGameRecordsByGameNumber(gameResult.getGameNumber(),loginUser.getUserId()));
            response.add("gameNumber",gameResult.getGameNumber());
        }
        return response;
    }
    @PostMapping(value = "/getResult", produces = {"application/json;charset=UTF-8"})
    public Response getResult(@Logined LoginUser loginUser,Integer gameNumber){
        GameResult gameResult = zhuanPanService.getGameResul(gameNumber);
        List<GameRecord> gameRecords = zhuanPanService.selectGameRecordsByGameNumber(gameResult.getGameNumber(),loginUser.getUserId());
        double resultChipAll = 0;
        for (GameRecord gameRecord : gameRecords) {
            if(resultChipAll+gameRecord.getResultChip()>0) {
                resultChipAll = resultChipAll + gameRecord.getResultChip()-gameRecord.getChip();
            }else{
                resultChipAll = resultChipAll - gameRecord.getChip();
            }
        }
        double yue = capitalAccountService.selectByUserId(loginUser.getUserId()).getYue();
        Response response = Response.success();
        response.add("gameRecords",gameRecords);
        response.add("result",gameResult.getResult());
        response.add("yue",yue);
        response.add("resultChip",resultChipAll);
        response.add("isEndScene",zhuanPanService.isEndScene(gameNumber) ? 1 : 0);
        //获取新的一局详情
        GameResult newGameResult = zhuanPanService.selectNewGameNumber();
        response.add("gameStatus",newGameResult!=null ? 1 : 0);
        if(newGameResult!=null){
            //计算下注秒数
            long interval = (new Date().getTime() - newGameResult.getCreateDate().getTime())/1000;
            if(interval>xiazhu_second){
                response.add("xiazhuSecond",0);
            }else{
                response.add("xiazhuSecond",xiazhu_second-interval);
            }
            //计算开奖秒数
            if(interval>xiazhu_second+kaijiang_second){
                response.add("kaijiangSecond",0);
            }else{
                response.add("kaijiangSecond",xiazhu_second+kaijiang_second-interval);
            }
            response.add("gameNumber",newGameResult.getGameNumber());
        }
        return response;
    }
    @PostMapping(value = "/xiaZhu", produces = {"application/json;charset=UTF-8"})
    public Response xiaZhu(@Logined LoginUser loginUser, @RequestBody List<GameRecord> gameRecords){
        zhuanPanService.xiaZhu(gameRecords,loginUser);
        return Response.success();
    }

    public static void main(String[] args) {
        double a = 40.0;
        double b = 10.0;
        System.out.println(a*0.95);
        System.out.println(b*0.95);
        int num = (int) (Math.random() * 4 + 1);
        System.out.println(num);
    }
}
